package logic

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"we-chat-mgr/models/mysql"

	"we-chat-mgr/wechatrpc/internal/svc"
	"we-chat-mgr/wechatrpc/wechatrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PerfectLogic {
	return &PerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PerfectLogic) Perfect(in *wechatrpc.PerfectRequest) (*wechatrpc.PerfectResponse, error) {
	// todo: add your logic here and delete this line

	var finduser mysql.User
	err := finduser.FindUserByUserId(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("用户不存在")
	}

	user := &mysql.User{
		Model:      gorm.Model{ID: uint(in.UserId)},
		WeChatCode: in.WeChatCode,
		Qq:         in.Qq,
		Email:      in.Email,
	}
	err = user.UpdateUser(mysql.DB)
	if err != nil {
		return nil, err
	}

	return &wechatrpc.PerfectResponse{
		UserId: int64(user.ID),
	}, nil
}
